package com.letv.work.video;

import java.sql.Connection;
import java.text.MessageFormat;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.letv.bean.SummaryBean;
import com.letv.common.ConnectionManager;
import com.letv.conf.ConfigFactory;
import com.letv.dataprocess.DataprocessService;
import com.letv.dataprocess.DataprocessServiceImpl;
import com.letv.dataprocess.Mappings;
import com.letv.download.DownloadService;
import com.letv.download.DownloadServiceImpl;
import com.letv.mail.MailSenderInfo;
import com.letv.util.DateUtil;
import com.letv.util.SQLUtil;
import com.letv.work.AbstarctWorkJob;

public class VideoJob extends AbstarctWorkJob {

	private static final Logger log = LoggerFactory.getLogger(VideoJob.class);

	@Override
	public SummaryBean work(String type) {
		long startT = System.currentTimeMillis();
		String startTStr = DateUtil.getInstance().formatTime(startT);
		long stepT = System.currentTimeMillis();
		String stepTStr = DateUtil.getInstance().formatTime(stepT);

		SummaryBean summary = new SummaryBean();
		MailSenderInfo mailInfo = new MailSenderInfo();

		mailInfo.setSubject(MessageFormat.format(ConfigFactory.getString("mail.subject"), type));

		StringBuilder subject = new StringBuilder("新媒资离线索引模块，全量任务执行情况如下:<br><br>");
		StringBuilder detail = new StringBuilder("");

		subject.append("服务器IP:" + ConfigFactory.getString("mail.host") + "<br><br>");
		subject.append("开始时间:" + startTStr + "<br><br>");

		if (Mappings.DICTIONARY_MAP == null || Mappings.DICTIONARY_MAP.size() == 0) {
			ConnectionManager cm = null;
			Connection connToSrc = null;
			try {
				cm = ConnectionManager.getInstance();
				connToSrc = cm.getConnectionFromMMS();
				SQLUtil.setDictionary(connToSrc);
				SQLUtil.setStar(connToSrc);
				SQLUtil.setRAlbumMap(connToSrc);
			} catch (Exception e) {
				log.error("init dictionary and star map error!", e);
				summary.setErrorMsg("init dictionary and star map error!");
				return summary;
			} finally {
			    SQLUtil.close(connToSrc);
			}
		}

		DownloadService dsi = new DownloadServiceImpl();
		DataprocessService dps = new DataprocessServiceImpl();

		try {
			log.info("begin download data:" + type);
			SummaryBean downResult = dsi.down(type);
			log.info("download " + type + " done!cost:" + (System.currentTimeMillis() - stepT)
			        / 1000 + "s begin process data:" + type);
			detail.append("--------下载模块---------------------------<br><br>");
			detail.append("成功下载数据:" + downResult.getSubTaskMysqlSuccessedCountNum() + "条<br><br>");
			detail.append("下载失败数据:" + downResult.getSubTaskMysqlFailedCountNum() + "条<br><br>");
			detail.append("耗时:" + (System.currentTimeMillis() - stepT) / 1000 + "s<br><br>");
			stepT = System.currentTimeMillis();

			SummaryBean processResult = dps.process(type);
			log.info("process " + type + " done!cost:" + (System.currentTimeMillis() - stepT)
			        / 1000 + "s begin indexing data:" + type);
			detail.append("--------汇聚模块---------------------------<br><br>");
			detail.append("成功汇聚数据:" + processResult.getSubTaskMysqlSuccessedCountNum()
			        + "条<br><br>");
			detail.append("汇聚失败数据:" + processResult.getSubTaskMysqlFailedCountNum() + "条<br><br>");
			detail.append("CBase入库数据:" + processResult.getSubTaskCbaseSuccessedCountNum()
			        + "条<br><br>");
			detail.append("CBase入库失败数据:" + processResult.getSubTaskCbaseFailedCountNum()
			        + "条<br><br>");
			detail.append("耗时:" + (System.currentTimeMillis() - stepT) / 1000 + "s<br><br>");
			stepT = System.currentTimeMillis();

			stepTStr = DateUtil.getInstance().formatTime(stepT);
			subject.append("结束时间:" + stepTStr + "<br><br>");
			subject.append(type + " 执行全量  总耗时:" + (stepT - startT) / 1000 + "s<br><br>");
		} catch (Exception e) {
			stepT = System.currentTimeMillis();
			stepTStr = DateUtil.getInstance().formatTime(stepT);
			log.error("执行出错！", e);
			subject.append("结束时间:" + stepTStr + "<br><br>");
			subject.append(type + " 执行全量出错:" + e.getMessage() + "  总耗时:" + (stepT - startT) / 1000
			        + "s<br><br>");
		}
		// subject.append("====================================================="+"<br><br>");
		mailInfo.setContent(subject.toString() + detail.toString());
		// send mail
		sendMail(mailInfo);

		log.info("send " + type + " mail done!");

		return summary;
	}

}
